# -*- coding: UTF-8 -*-
'''
Created on 05.02.2013

@author: p.hess
'''
import sys

from LinearSpreadGen import LinearSpread 
from LinearSpreadGen import SpreadType
from Transform import Transform
from Objects3D import Quad
from Animation import LFO
from pi3d import Display
from pi3d.Camera import Camera
from pi3d.Keyboard import Keyboard
from pi3d.util.Screenshot import screenshot

from time import time


class test():
    
    def __init__(self):
        self.KeyboardQueue = None
        
    def setup(self):
        
        #
        # Setup Pi3D
        #
        
        self.DISPLAY = Display.create(0, 0) 
        # now we need a background color. We select black and a 
        # non opaque 
        self.DISPLAY.set_background(0, 0, 0, 1)  # r,g,b,alpha -> black background
        
        #
        # DISPLAY.frames_per_second
        #
        self.DISPLAY.frames_per_second = 25
        
        #
        # Setup keyboard handling
        #

        self.mykeys = Keyboard()        

        print "keyboard handling established" + '\n\r'


    def teardown(self):
        
        #
        # ensure, that all output is written to the display
        #
        sys.stdout.flush()
        
        #
        # down Pi3d
        #    
        self.DISPLAY.destroy()
        print "teardown Pi3d done"  + '\n\r'           
  
  
    def loop(self):    

        print "enter main loop"

        self.prepareSpreadableObjects()
        
        # do the first inisitalisation        
        
        self.keyboardControlledSpreadcount = 5
        self.keyboardControlledRotation = 0.0
        
        lasttime = time()
        loopCounter = 0
        
        while self.DISPLAY.loop_running():
            loopCounter += 1            
            
            self.computeFrame();
            
            # read keyboard not every loop. Slows down the loop            

            if (loopCounter % 1 == 0) :
                                
                k = self.mykeys.read()
                if k > -1:
                    if k == 112: screenshot("earthPic.jpg")    
                    elif k == 27: break # do end processing, finito, go to weekend
                    elif k==119:  # key w
                        self.keyboardControlledRotation -= 0.1
                        if (self.keyboardControlledRotation < 0): self.keyboardControlledRotation = 1
                    elif k==115: # key s
                        self.keyboardControlledRotation += 0.1
                        if (self.keyboardControlledRotation > 1): self.keyboardControlledRotation = 0
                    elif k == 97:  # key a
                        self.keyboardControlledSpreadcount -= 1 
                        if (self.keyboardControlledSpreadcount < 1): self.keyboardControlledSpreadcount = 1
                    elif k == 100: self.keyboardControlledSpreadcount += 1  # keay d
                    else: 
                        print "not processed key:" + str(k) + '\n\r'
    
                if (loopCounter % 100 == 0) :
                    currtime = time()
                    loopsPerSecond = 100.0 / (currtime - lasttime)
                    sys.stdout.write(str(time()) + "-" + str(loopCounter) + "-lps: " + str(loopsPerSecond) + '\n\r')
                    lasttime = currtime

            Camera.instance().was_moved = False
            
        print "exit main loop"  + '\n\r'


    def prepareSpreadableObjects(self):
        '''Creates all the objects which are used in the computeFrame method'''
        
        # creates a value between 0 and 1 upcounding within one second
        # not spreadable in this version
        self.LFO = LFO(Period_in=1.0)
        
        # creates a spread with n values which are used for positioning
        self.LinearSpread = LinearSpread(SpreadCount_in=5, 
                                         SpreadType_in=SpreadType.Centered)
        # creates a spread to contel the size (scale) of the objects
        self.LinearSpreadScale = LinearSpread(Width_in=.2, 
                                              SpreadCount_in=5, 
                                              SpreadType_in=SpreadType.Centered)
        # prepares scaling and positioning
        self.Transform = Transform(translateX_in=None, 
                                   translateY_in=[0], 
                                   scaleX_in=[0.1], 
                                   scaleY_in=[0.1])
        # he builder of the Quads (planes)
        self.Quad = Quad()


    def computeFrame(self):
        '''compute the next frame to show'''
        self.LinearSpread.update(SpreadCount_in=self.keyboardControlledSpreadcount)
        self.LinearSpreadScale.update()
        self.LFO.update()
        
        # print "self.LinearSpreadScale.output:", self.LinearSpreadScale.output
        self.Transform.update(translateX_in=self.LinearSpread.output,
                              translateY_in=self.LinearSpread.output,
                              scaleX_in=self.LinearSpreadScale.output,
                              scaleY_in=self.LinearSpreadScale.output,
                              rotate_in=self.LFO.output)
        self.Quad.update(transform=self.Transform.transform_out)

# end class --------------------------------------------------------------------------------------------
    
mytest = test()
mytest.setup()
mytest.loop()
mytest.teardown()
